package com.md24housecall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.md24housecall.adapter.IHealthPagerAdapter;
import com.md24housecall.fragment.ActivityReportFragment;
import com.md24housecall.fragment.BloodGlucoFragment;
import com.md24housecall.fragment.BloodOxygenFragment;
import com.md24housecall.fragment.BloodPressureFragment;
import com.md24housecall.fragment.WeightFragment;
import com.md24housecall.util.GlobalConstant;

public class IHealthActivity extends FragmentActivity implements View.OnClickListener{
	private ViewPager mPager;
	private IHealthPagerAdapter adapter;
//	private CacheManager cacheManager;
	private Fragment[] arrayFragment={new BloodPressureFragment(),new WeightFragment(),new BloodGlucoFragment(),new BloodOxygenFragment(),new ActivityReportFragment()};
	private ImageButton tab_pressure,tab_weight,tab_gluco,tab_oxygen,tab_activity_tracker;
	private int width,height;
	private Button btnBack,btnAccount;
	private TextView txvIhealthName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ihealth);
		try{
			initialize();
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void initialize() {
		btnBack=(Button)findViewById(R.id.btnBack);
		txvIhealthName=(TextView)findViewById(R.id.txvIhealthName);
		String name=getIntent().getStringExtra(GlobalConstant.getInstance().IHEALTH_NAME_KEY);
		if(name!=null && !name.equals("")){
			txvIhealthName.setText(name);
		}
		btnBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		
		btnAccount=(Button)findViewById(R.id.btnAccount);
//		btnAccount.setText(getString(R.string.button_quick_add));
		if(GlobalConstant.getInstance().userType==GlobalConstant.getInstance().CENTER_USER)
			btnAccount.setVisibility(View.GONE);
		btnAccount.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(IHealthActivity.this,LoginActivity.class));
				finish();
			}
		});
		
		mPager = (ViewPager)findViewById(R.id.viewPager);
		width=getResources().getDisplayMetrics().widthPixels/5;
		height=(width*100)/200;
		tab_pressure=(ImageButton)findViewById(R.id.tabPressure);
		tab_pressure.setOnClickListener(this);
		
		tab_weight=(ImageButton)findViewById(R.id.tabWeight);
		tab_weight.setOnClickListener(this);
		
		tab_gluco=(ImageButton)findViewById(R.id.tabGluco);
		tab_gluco.setOnClickListener(this);
		
		tab_oxygen=(ImageButton)findViewById(R.id.tabOxygen);
		tab_oxygen.setOnClickListener(this);
		
		tab_activity_tracker=(ImageButton)findViewById(R.id.tabActivityTracker);
		tab_activity_tracker.setOnClickListener(this);
		
		tab_pressure.getLayoutParams().width=width;
		tab_pressure.getLayoutParams().height=height;
		tab_weight.getLayoutParams().width=width;
		tab_weight.getLayoutParams().height=height;
		tab_gluco.getLayoutParams().width=width;
		tab_gluco.getLayoutParams().height=height;
		tab_oxygen.getLayoutParams().width=width;
		tab_oxygen.getLayoutParams().height=height;
		tab_activity_tracker.getLayoutParams().width=width;
		tab_activity_tracker.getLayoutParams().height=height;
		onTabPressed(0);
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int index) {
				// TODO Auto-generated method stub
//				MyLog.d(GlobalConstant.TAG, "onPageSelected: "+index);
				onTabPressed(index);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
//				MyLog.d(GlobalConstant.TAG, "onPageScrolled");
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
//				MyLog.d(GlobalConstant.TAG, "onPageScrollStateChanged");
			}
		});
		mPager.setOffscreenPageLimit(arrayFragment.length);
		adapter=new IHealthPagerAdapter(getSupportFragmentManager(), arrayFragment);
		mPager.setAdapter(adapter);
//		
	}
	
	private void onTabPressed(int index){
		tab_pressure.setImageResource(R.drawable.tab_pressure);
		tab_weight.setImageResource(R.drawable.tab_weight);
		tab_gluco.setImageResource(R.drawable.tab_gluco);
		tab_oxygen.setImageResource(R.drawable.tab_oxygen);
		tab_activity_tracker.setImageResource(R.drawable.tab_activity_tracker);
		switch (index) {
		case 0:
			tab_pressure.setImageResource(R.drawable.tab_selected_pressure);
			break;
		case 1:
			tab_weight.setImageResource(R.drawable.tab_selected_weight);
			break;
		case 2:
			tab_gluco.setImageResource(R.drawable.tab_selected_gluco);
			break;
		case 3:
			tab_oxygen.setImageResource(R.drawable.tab_selected_oxygen);
			break;
		case 4:
			tab_activity_tracker.setImageResource(R.drawable.tab_selected_activity_tracker);
			break;
		default:
			tab_pressure.setImageResource(R.drawable.tab_selected_pressure);
			break;
		}
	}
	
	private void switchTab(int index){
		mPager.setCurrentItem(index, true);
		onTabPressed(index);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tabPressure:
			switchTab(0);
			break;
		case R.id.tabWeight:
			switchTab(1);
			break;
		case R.id.tabGluco:
			switchTab(2);
			break;
		case R.id.tabOxygen:
			switchTab(3);
			break;
		case R.id.tabActivityTracker:
			switchTab(4);
			break;
		default:
			switchTab(0);
			break;
		}
	}
}
